package com.example.langchain4j_spring.controller;

import com.example.langchain4j_spring.config.AiConfig;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.service.TokenStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.LocalDateTime;

/**
 * @Author: zjg
 * @Date: 2025/5/13 10:01
 * @Description:
 **/
@RestController
@RequestMapping("/ai")
public class ChatController {

    @Autowired
    QwenChatModel qwenChatModel;

    @Autowired
    QwenStreamingChatModel qwenStreamingChatModel;


    @Autowired
    AiConfig.Assistant assistant;

    @Autowired
    AiConfig.AssistantUnique ass;

    @RequestMapping("/chat")
    public String chat(@RequestParam(defaultValue = "你是谁？") String question) {
        String chat = qwenChatModel.chat(question);
        return chat;
    }

    @RequestMapping(value = "/stream", produces = "text/stream;charset=UTF-8")
    public Flux<String> stream(@RequestParam(defaultValue = "你是谁？") String question) {
        Flux<String> objectFlux = Flux.create(emitter -> {
            qwenStreamingChatModel.chat(question, new StreamingChatResponseHandler() {
                @Override
                public void onPartialResponse(String s) {
                    emitter.next(s);
                }

                @Override
                public void onCompleteResponse(ChatResponse chatResponse) {
                    emitter.complete();
                }

                @Override
                public void onError(Throwable throwable) {
                    emitter.error(throwable);
                }
            });
        });


        return objectFlux;
    }


    /**
     * ChatMemory记录对话
     * ****/

    @RequestMapping("/memory_chat")
    public String memory_chat(@RequestParam(defaultValue = "我是张建涛") String question) {
        return assistant.chat(question);
    }

    @RequestMapping(value = "/memory_stream", produces = "text/stream;charset=UTF-8")
    public Flux<String> memoryStreamChat(@RequestParam(defaultValue = "我是谁？") String question){
        TokenStream stream =assistant.stream(question);
        return Flux.create(sink -> {
            stream.onPartialResponse(s -> sink.next(s))
                    .onCompleteResponse(chatResponse -> sink.complete())
                    .onError(throwable -> sink.error(throwable))
                    .start();
        });
    }



    /**
     * ChatMemory记录对话，加对话隔离，多个用户不会看到对方对话记录
     * ****/

    @RequestMapping("/memoryId_chat")
    public String memoryId_chat(@RequestParam(defaultValue = "我是张建涛") String question,Integer userId) {
        return ass.chat(userId,question);
    }

    @RequestMapping(value = "/memoryId_stream", produces = "text/stream;charset=UTF-8")
    public Flux<String> memoryIdStreamChat(@RequestParam(defaultValue = "我是谁？") String question,Integer userId){
        TokenStream stream =ass.stream(userId,question, LocalDateTime.now().toString());
        return Flux.create(sink -> {
            stream.onPartialResponse(s -> sink.next(s))
                    .onCompleteResponse(chatResponse -> sink.complete())
                    .onError(throwable -> sink.error(throwable))
                    .start();
        });
    }


    /**
     * 对话记忆持久化
     * ****/

}
